package com.yupi.yuojbackendjudgeservice.judge.codesanbox.impl;

import cn.hutool.http.HttpRequest;
import cn.hutool.json.JSONUtil;
import com.yupi.yuojbackendmodel.codesanbox.ExecuteCodeRequest;
import com.yupi.yuojbackendmodel.codesanbox.ExecuteCodeResponse;
import com.yupi.yuojbackendcommon.common.ErrorCode;
import com.yupi.yuojbackendcommon.exception.BusinessException;
import com.yupi.yuojbackendjudgeservice.judge.codesanbox.CodeSanBox;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

/**
 * @Author FengHuan Wang
 * @Date 2025/3/9 9:48
 * @Version 1.0
 */

/**
 * 远程代码沙箱（实际调用）
 */
@Slf4j
public class RemoteSanBox implements CodeSanBox {

    private static final String AUTH_REQUEST_HEADER = "auth";

    private static final String AUTH_REQUEST_SECURITY = "secretKey";

    @Override
    public ExecuteCodeResponse executeCode(ExecuteCodeRequest executeCodeRequest) {
        log.info("调用远程代码沙箱");
        String url = "http://59.110.20.114:9090/executeCode";
        String json = JSONUtil.toJsonStr(executeCodeRequest);
        String resStr = HttpRequest.post(url)
                .header("Authorization", AUTH_REQUEST_HEADER)  // 设置授权头
                .body(json)  // 设置请求体
                .execute()  // 发送请求并获取HttpResponse对象
                .body();  // 获取响应体内容
        if (StringUtils.isBlank(resStr)){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, "调用代码沙箱API失败" + resStr);
        }
        return JSONUtil.toBean(resStr, ExecuteCodeResponse.class);
    }


}
